/*
 * Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package edu.vital.energy.longevity.vitalenergy.code.generator.code;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Main
 *
 * @version 1.0.1
 * @auther FengJinJian
 * @date 2024/11/24 22:22
 */
public class Main {
    public static void main(String[] args) {
        String url="jdbc:mysql://127.0.0.1:3306/online_lab?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false";
        String username="root";
        String password="Hbcit@2025";
        String outputDir = "C:\\Users\\Administrator\\IdeaProjects\\lab3\\";
        String packageName = "edu.vital.energy.longevity.";
        String[] tables = new String[]{"contract"};
        String tableComment="合同";
        String moduleName="contract";
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author("Feng JinJian")
                        .outputDir(outputDir+ "/src/main/java/")
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent("edu.vital.energy.longevity")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .addInclude(tables)
                        .entityBuilder()
                        .enableLombok()
                        .enableFileOverride()
                        .controllerBuilder()
                        .enableFileOverride()
                        .enableRestStyle()
                        .mapperBuilder()
                        .enableFileOverride()
                        .enableBaseColumnList()
                        .enableBaseResultMap()
                        .serviceBuilder()
                        .enableFileOverride()
                )
                .injectionConfig(getProfiledTemplates())
                 .injectionConfig(getOtherMap(tableComment,tables,moduleName))
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();


    }
    @NotNull
    private static Consumer<InjectionConfig.Builder> getProfiledTemplates() {
        return consumer -> {
            consumer.customFile(new TemplateInfo().getCustomFiles());
        };
    }
    private static Consumer<InjectionConfig.Builder> getOtherMap(String tableComment,String[] tables,String moduleName) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        objectMap.put("httpComment", "###");
        // 版本
        // api 注释
        Map<String, String> apiComment = new HashMap<>(12);
        apiComment.put("create", "创建" + tableComment);
        apiComment.put("updateAll", "根据条件更新" + tableComment);
        apiComment.put("updateById", "更新" + tableComment);
        apiComment.put("getById", "查询" + tableComment);
        apiComment.put("getByIdList", "批量查询" + tableComment);
        apiComment.put("queryList", "查询全部" + tableComment);
        apiComment.put("queryListByCache", "通过缓存查询" + tableComment);
        apiComment.put("queryPage", "分页查询" + tableComment);
        apiComment.put("queryCount", "查询" + tableComment + "数量");
        apiComment.put("deleteById", "删除" + tableComment);
        apiComment.put("deleteAll", "根据条件删除" + tableComment);
        apiComment.put("batchDelete", "批量删除" + tableComment);
        apiComment.put("typeHyphen", tables[0]);
        apiComment.put("ModuleName", moduleName);
        objectMap.put("apiComment", apiComment);
        return consumer -> {
            consumer.customMap(objectMap);
        };
    }
}